package com.lonely.fx.mouse;

import javafx.application.Application;
import javafx.beans.binding.DoubleBinding;
import javafx.beans.property.SimpleIntegerProperty;
import javafx.event.EventHandler;
import javafx.event.EventType;
import javafx.geometry.Insets;
import javafx.scene.Scene;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;
import javafx.scene.input.MouseButton;
import javafx.scene.input.MouseEvent;
import javafx.scene.layout.HBox;
import javafx.stage.Stage;

/**
 * @date: 2022-07-27 13:34
 * @author: ztkj-hzb
 * @description: 鼠标事件 属性设置
 **/
public class MouseEventSettingMain extends Application {

    public static void main(String[] args) {
        launch(args);
    }

    /**
     *
     */
    @Override
    public void start(Stage primaryStage) throws Exception {


        HBox hBox = new HBox();
        hBox.setPadding(new Insets(50));

        Image image = new Image("icons/welcome.jpg");
        ImageView imageView = new ImageView(image);
        imageView.setPreserveRatio(true);


        //设置鼠标单击事件
        imageView.setOnMouseClicked(event -> {

            System.out.println("触发 mouseClicked 事件");

            //获取事件源
            Object source = event.getSource();
            //System.out.println("事件源：" + source);

            //System.out.println("相对组件坐标位置--x：" + event.getX());
            //System.out.println("相对组件坐标位置--y：" + event.getY());
            //System.out.println("相对布局坐标位置--x：" + event.getSceneX());
            //System.out.println("相对布局坐标位置--y：" + event.getSceneY());
            //System.out.println("相对屏幕坐标位置--x：" + event.getScreenX());
            //System.out.println("相对屏幕坐标位置--y：" + event.getScreenY());

            //获取事件类型
            EventType<? extends MouseEvent> eventType = event.getEventType();
            //System.out.println("事件类型：" + eventType);

            //获取点击次数
            int clickCount = event.getClickCount();
            //System.out.println("点击次数：" + clickCount);

            //获取是哪个鼠标按钮进行触发的事件
            MouseButton mouseButton = event.getButton();
            //System.out.println("鼠标按钮：" + mouseButton.name());
            //System.out.println("是否是鼠标左键"+event.isPrimaryButtonDown());
            //System.out.println("是否是鼠标右键"+event.isSecondaryButtonDown());
            //System.out.println("是否是鼠标中键"+event.isMiddleButtonDown());

        });

        //鼠标按下事件，如果鼠标按钮不松开，则会一直触发
        imageView.setOnMousePressed(new EventHandler<MouseEvent>() {
            @Override
            public void handle(MouseEvent event) {
                System.out.println("触发 mousePressed 事件");
                System.out.println("是否同上次落点一样："+event.isStillSincePress());
            }
        });

        //鼠标松开事件
        imageView.setOnMouseReleased(new EventHandler<MouseEvent>() {
            @Override
            public void handle(MouseEvent event) {
                System.out.println("触发 mouseReleased 事件");
            }
        });

        //鼠标进入组件范围触发
        imageView.setOnMouseEntered(new EventHandler<MouseEvent>() {
            @Override
            public void handle(MouseEvent event) {
                System.out.println("触发 mouseEntered 事件");
            }
        });

        //鼠标离开组件范围触发
        imageView.setOnMouseExited(new EventHandler<MouseEvent>() {
            @Override
            public void handle(MouseEvent event) {
                System.out.println("触发 mouseExited 事件");
            }
        });

        //鼠标在组件范围内移动触发事件
        imageView.setOnMouseMoved(new EventHandler<MouseEvent>() {
            @Override
            public void handle(MouseEvent event) {
                System.out.println("触发 mouseMoved 事件");
            }
        });


        //hbox也设置了 鼠标单击事件，此时看到控制台既会触发iamgeview的单击事件，也会触发hbox的单击事件
        //为什么都会触发，因为事件传递导致，父事件传递给了子事件，可以通过设置禁止传递
        //设置组织事件传递
        //imageView.setMouseTransparent(true);
        hBox.setOnMouseClicked(new EventHandler<MouseEvent>() {
            @Override
            public void handle(MouseEvent event) {
                System.out.println("触发 hbox mouseClicked 事件");
            }
        });

        hBox.getChildren().addAll(imageView);
        primaryStage.setScene(new Scene(hBox));
        primaryStage.setWidth(600);
        primaryStage.setHeight(500);
        primaryStage.show();

        //设置imageview的宽度跟屏幕的一半
        DoubleBinding widthBind = hBox.widthProperty().divide(2);
        imageView.fitWidthProperty().bind(widthBind);
    }
}
